package week_three_day03.sax;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import week_three_day03.dom.Book;

public class BookHandler extends DefaultHandler {
	Book book = null;
	ArrayList<Book> books;
	private String tag;
	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		books = new ArrayList<Book>();
	}


	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if("book".equals(qName)){
			book = new Book();
			book.setTyPe(attributes.getValue("Type"));
			book.setISbN(attributes.getValue("isbn"));
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if("book".equals(qName)){
			books.add(book);
		}
		tag = null;
	}
	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String s = new String(ch,start,length);
		if("author".equals(tag)){
			book.setAuthor(s);
		}
	}
	
}
